package com.xmut.bs.handaccount.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.xmut.bs.handaccount.R;


/**
 * 自定义的RecycleViewDivider
 * Author Dollkey
 * Date 2017/9/22 16:31.
 */

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    //水平
    public static final int HORIZONTAL_LIST = RecyclerView.HORIZONTAL;
    //垂直
    public static final int VERTICAL_LIST = RecyclerView.VERTICAL;
    //水平+垂直
    public static final int BOTH_SET = 2;

    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int TOP = 3;
    public static final int BOTTOM = 4;
    public static final int CENTER = 0;

    private int mOrientation;
    private int mDividerLength;
    private Paint mPaint;
    private int mSpanCount;

    private boolean mHasTopAndBottom;
    private boolean mHasLeftOrRight;

    public DividerItemDecoration(Context context) {
        this(context, context.getResources().getDimensionPixelSize(R.dimen.divider_height));
    }

    public DividerItemDecoration(Context context, int dividerLength) {
        this(context, dividerLength, VERTICAL_LIST, 1, true);
    }

    public DividerItemDecoration(Context context, int dividerLength, int color) {
        this(context, dividerLength, VERTICAL_LIST, 1, true,color);
    }

    public DividerItemDecoration(Context context, int dividerLength, int orientation, boolean hasTopAndBottom) {
        this(context, dividerLength, orientation, 1, hasTopAndBottom);
    }

    public DividerItemDecoration(Context context, int dividerLength, int orientation, int spanCount, boolean hasTopAndBottom) {
        this(context, dividerLength, orientation, spanCount, hasTopAndBottom, context.getResources().getColor(R.color.line_grey_10));
    }

    public DividerItemDecoration(Context context, int dividerLength, int orientation, int spanCount, boolean hasTopAndBottom, int color) {
        mSpanCount = spanCount;
        mHasTopAndBottom = hasTopAndBottom;
        mPaint = new Paint();
        mOrientation = orientation;
        mPaint.setColor(color);
        mDividerLength = dividerLength;
        mHasLeftOrRight = true;
    }

    public DividerItemDecoration setDividerLength(int dividerLength) {
        mDividerLength = dividerLength;
        return this;
    }

    public DividerItemDecoration setHasTopAndBottom(boolean hasTopAndBottom) {
        mHasTopAndBottom = hasTopAndBottom;
        return this;
    }

    public DividerItemDecoration setHasLeftOrRight(boolean hasLeftOrRight) {
        mHasLeftOrRight = hasLeftOrRight;
        return this;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //下面super...代码其实调用的就是那个过时的getItemOffsets,也就是说这个方法体内容也可以通通移到那个过时的getItemOffsets中
        super.getItemOffsets(outRect, view, parent, state);
        //获取layoutParams参数
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
        //当前位置
        int itemPosition = layoutParams.getViewLayoutPosition();
        //ItemView数量
        int childCount = parent.getAdapter().getItemCount();

        int dividerLength = mDividerLength / 2;
        int left = dividerLength;
        int right = dividerLength;
        int top = dividerLength;
        int bottom = dividerLength;

        switch (mOrientation) {
            case BOTH_SET:
                if (isTopOrBottom(itemPosition, childCount) == TOP) {
                    if (mHasTopAndBottom)
                        top = mDividerLength;
                    else
                        top = 0;
                } else if (isTopOrBottom(itemPosition, childCount) == BOTTOM) {
                    if (mHasTopAndBottom)
                        bottom = mDividerLength;
                    else
                        bottom = 0;
                }
                if (isLeftOrRight(itemPosition) == LEFT) {
                    if (mHasLeftOrRight)
                        left = mDividerLength;
                    else
                        left = 0;
                } else if (isLeftOrRight(itemPosition) == RIGHT) {
                    if (mHasLeftOrRight)
                        right = mDividerLength;
                    else
                        right = 0;
                }
                outRect.set(left, top, right, bottom);
                break;
            case HORIZONTAL_LIST:
                childCount -= 1;
                top = bottom = 0;
                if (mHasTopAndBottom) {
                    top = bottom = mDividerLength;
                }
                left = 0;
                right = mDividerLength;
                if (mHasLeftOrRight) {
                    if (itemPosition == 0)
                        left = mDividerLength;
                    else if (itemPosition == childCount)
                        right = 0;
                }
                outRect.set(left, top, right, bottom);
                break;
            case VERTICAL_LIST:
                childCount -= 1;
                outRect.set(0, 0, 0, (itemPosition != childCount) ? mDividerLength : 0);
                left = right = 0;
                if (mHasLeftOrRight) {
                    left = right = mDividerLength;
                }
                top = 0;
                bottom = mDividerLength;
                if (mHasTopAndBottom) {
                    if (itemPosition == 0)
                        top = mDividerLength;
                    else if (itemPosition == childCount)
                        bottom = 0;
                }
                outRect.set(left, top, right, bottom);
                break;
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        if (mOrientation == VERTICAL_LIST) {
            drawVertical(c, parent);
        } else if (mOrientation == HORIZONTAL_LIST) {
            drawHorizontal(c, parent);
        } else {
            drawHorizontal(c, parent);
            drawVertical(c, parent);
        }
    }


    /**
     * 绘制横向 item 分割线
     *
     * @param canvas 画布
     * @param parent 父容器
     */
    private void drawVertical(Canvas canvas, RecyclerView parent) {
        int x = parent.getPaddingLeft();
        final int width = parent.getMeasuredWidth() - parent.getPaddingRight();
        //getChildCount()(ViewGroup.getChildCount) 返回的是显示层面上的“所包含的子 View 个数”。
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams =
                    (RecyclerView.LayoutParams) child.getLayoutParams();
            //item底部的Y轴坐标+margin值
            final int y = child.getBottom() + layoutParams.bottomMargin;
            final int height = y + mDividerLength;
            if (mPaint != null) {
                if (isTopOrBottom(i, childSize) == TOP) {
                    canvas.drawRect(x, 0, width, mDividerLength, mPaint);
                }
                canvas.drawRect(x, y, width, height, mPaint);
            }
        }
    }

    /**
     * 绘制纵向 item 分割线
     *
     * @param canvas
     * @param parent
     */
    private void drawHorizontal(Canvas canvas, RecyclerView parent) {
        final int top = parent.getPaddingTop();
        final int bottom = parent.getMeasuredHeight() - parent.getPaddingBottom();
        final int childSize = parent.getChildCount();
        for (int i = 0; i < childSize; i++) {
            final View child = parent.getChildAt(i);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left = child.getRight() + layoutParams.rightMargin;
            int right = left + mDividerLength;
            if (mPaint != null) {
                if (isLeftOrRight(i) == LEFT) {
                    canvas.drawRect(0, top, mDividerLength, bottom, mPaint);
                }
                canvas.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    /**
     * 获取列数
     *
     * @param parent
     * @return
     */
    private int getSpanCount(RecyclerView parent) {
        int spanCount = -1;
        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }
        return spanCount;
    }

    private int isLeftOrRight(int position) {
        if (mSpanCount == 1) {
            return CENTER;
        }
        if (position % mSpanCount == 0) {
            return LEFT;
        } else if (position % mSpanCount == mSpanCount - 1) {
            return RIGHT;
        }
        return CENTER;
    }

    private int isTopOrBottom(int position, int childCount) {
        if (mSpanCount == 1) {
            if (position == 0)
                return TOP;
            if (position == childCount - 1)
                return BOTTOM;
            return CENTER;
        }
        if (position < mSpanCount) {
            return TOP;
        }
        if (childCount % mSpanCount != 0) {
            childCount = childCount + mSpanCount - childCount % mSpanCount;
        }
        if (childCount - position - 1 < mSpanCount) {
            return BOTTOM;
        }
        return CENTER;
    }
}
